package de.idgard.android.addon.ui.anim;

import android.app.Activity;
import android.content.Context;
import android.os.Vibrator;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import de.idgard.android.addon.services.GlobalServiceRegistry;
import de.idgard.android.addon.ui.services.LayoutAdjustmentService;
import de.idgard.android.addon.ui.services.ZircoToIDGardAPI;
import de.idgard.android.addon.ui.services.impl.UIServiceRegistry;

/**
 * @author pratik popat - Custom notification style IDGARD tool bar animation.
 * 
 */
public class ToolbarCustomAnimationMgr {

	private int bottom, left, right, top;
	private float startY, endY, defaultLayoutThreshold = 100;
	private boolean toolLayoutVisible = false;
	private long startTime, endTime;
	private final short VELOCITY_THRESOLD = 36;

	private Activity context;
	private LinearLayout idgardToolbarlayout, idgardLayoutOnMode;
	Vibrator vibrator;

	public ToolbarCustomAnimationMgr(Activity context,
			LinearLayout idgardMoretoolbarlayout,
			LinearLayout idgardBackgroundLayoutOnMode) {
		this.context = context;
		this.idgardToolbarlayout = idgardMoretoolbarlayout;
		this.idgardLayoutOnMode = idgardBackgroundLayoutOnMode;
		vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
	}

	private ZircoToIDGardAPI getZircoToIDGardAPI() {
		return GlobalServiceRegistry.instance().getZircoToIDGardAPI();
	}

	private LayoutAdjustmentService getLayoutAdjustmentService() {
		return UIServiceRegistry.instance().getlayouAdjustmentService();
	}

	public void handleToolbarTouch(MotionEvent event) {

		float y = event.getY();
		defaultLayoutThreshold = getLayoutAdjustmentService().getScreenHeight() * 25 / 100;

		if (MotionEvent.ACTION_DOWN == event.getAction()) {
			startY = event.getY();
			startTime = System.currentTimeMillis();
			setToolbarVisibility(true);
			moreToolbarLayoutAdj(y);

		}
		if (MotionEvent.ACTION_MOVE == event.getAction()) {
			moreToolbarLayoutAdj(y);
		}
		if (MotionEvent.ACTION_UP == event.getAction()) {
			endY = event.getY();
			endTime = System.currentTimeMillis();
			if (endY > startY + defaultLayoutThreshold
					|| calculateVelocity(startTime, endTime, startY, endY) > VELOCITY_THRESOLD) {
				fillToolbarLayout();
			} else if (startY > endY + defaultLayoutThreshold
					|| calculateVelocity(startTime, endTime, startY, endY) > VELOCITY_THRESOLD) {
				unfillToolbarLayout();
				setToolbarVisibility(false);
			} else {
				unfillToolbarLayout();
				setToolbarVisibility(false);
			}

		}

	}

	private void moreToolbarLayoutAdj(float y) {
		int l = idgardToolbarlayout.getLeft();
		int r = idgardToolbarlayout.getRight();
		int t = 0;
		int b = 0;
		if (!getZircoToIDGardAPI().getUrlBarVisibility())
			b = (int) y - idgardLayoutOnMode.getHeight();
		else
			b = (int) y - idgardLayoutOnMode.getHeight()
					- getZircoToIDGardAPI().getUrlBar().getHeight();

		if (b <= 0)
			b = 0;

		idgardToolbarlayout.layout(l, t, r, b);
	}

	public void handleToolbarpadTouch(MotionEvent event) {

		float y = event.getRawY();
		defaultLayoutThreshold = getLayoutAdjustmentService().getScreenHeight() * 25 / 100;

		if (MotionEvent.ACTION_DOWN == event.getAction()) {
			startY = event.getRawY();
			startTime = System.currentTimeMillis();
			moreToolbarLayoutAdj(y);
		}
		if (MotionEvent.ACTION_MOVE == event.getAction()) {
			moreToolbarLayoutAdj(y);
		}
		if (MotionEvent.ACTION_UP == event.getAction()) {
			endY = event.getRawY();
			endTime = System.currentTimeMillis();
			if (endY < startY - defaultLayoutThreshold
					|| calculateVelocity(startTime, endTime, startY, endY) > VELOCITY_THRESOLD) {
				unfillToolbarLayout();
				setToolbarVisibility(false);
			} else {
				fillToolbarLayout();
			}
		}
	}

	private float calculateVelocity(long startT, long endT, float startY,
			float endY) {
		long time = endT - startT;
		float distance = startY - endY;
		float velocity = 0;

		if (distance < 0)
			distance *= -1;

		if (distance > 0 && time > 0)
			velocity = distance / time;

		return velocity *= 100;
	}

	private void fillToolbarLayout() {
		Display display = context.getWindow().getWindowManager()
				.getDefaultDisplay();
		int height = display.getHeight();
		bottom = idgardToolbarlayout.getBottom();
		left = idgardToolbarlayout.getLeft();
		right = idgardToolbarlayout.getRight();
		top = 0;
		while (bottom <= height - 10) {
			idgardToolbarlayout.layout(left, top, right, bottom);
			bottom++;
		}
		toolLayoutVisible = true;
		vibrate();
	}
	private void vibrate(){
		vibrator.vibrate(100);
	}

	private void unfillToolbarLayout() {
		bottom = idgardToolbarlayout.getBottom();
		left = idgardToolbarlayout.getLeft();
		right = idgardToolbarlayout.getRight();
		top = 0;

		while (bottom >= top) {
			idgardToolbarlayout.layout(left, top, right, bottom);
			bottom--;
		}

		toolLayoutVisible = false;
		getZircoToIDGardAPI().startToolbarsHideRunnable();
	}

	public void setToolbarVisibility(boolean visible) {
		if (visible) {
			idgardToolbarlayout.setVisibility(View.VISIBLE);

		} else {
			unfillToolbarLayout();
			idgardToolbarlayout.setVisibility(View.INVISIBLE);
		}
	}

	public boolean getToolLayoutVisibility() {
		return toolLayoutVisible;
	}
}
